package com.example.base.repository;

import com.example.base.RequestQuery;
import com.example.base.model.BaseEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author libo
 * @Description //TODO Administrator
 * *****************************************************************************************************
 *  // 通过名称找到用户
        TestUser findByName(String name);

        // 通过名称和年龄找到用户
        TestUser findByNameAndAge(String name, Integer age);
        List<TestUser> findByNameOrAge(String name, Integer age);

        // 通过名称模糊查询
        List<TestUser> findByNameLike(String name);

        // 使用hql查询
         @Query("from TestUser u where u.name=:name")//:name对应@Param里的name
         TestUser findByHQL(@Param("name") String name);

         // 使用sql查询
         @Query(value = "select * from TestUser where name = ?1 and age = ?2", nativeQuery = true)//?1表示第一个参数，?2表示第二个参数
         TestUser findBySQL(String name, Integer age);
   *******************************************************************************************************
         Keyword                        Sample	                                                                JPQL snippet
         And	            findByLastnameAndFirstname	                                        … where x.lastname = ?1 and x.firstname = ?2
         Or	                findByLastnameOrFirstname	                                        … where x.lastname = ?1 or x.firstname = ?2
         Is,Equals	        findByFirstname,findByFirstnameIs,findByFirstnameEquals         	… where x.firstname = 1?
         Between	        findByStartDateBetween	                                            … where x.startDate between 1? and ?2
         LessThan	        findByAgeLessThan	                                                … where x.age < ?1
         LessThanEqual	    findByAgeLessThanEqual	                                            … where x.age <= ?1
         GreaterThan	    findByAgeGreaterThan                                            	… where x.age > ?1
         GreaterThanEqual	findByAgeGreaterThanEqual	                                        … where x.age >= ?1
         After	            findByStartDateAfter	                                            … where x.startDate > ?1
         Before         	findByStartDateBefore	                                            … where x.startDate < ?1
         IsNull         	findByAgeIsNull                                                 	… where x.age is null
         IsNotNull,NotNull	findByAge(Is)NotNull	                                            … where x.age not null
         Like	            findByFirstnameLike	                                                … where x.firstname like ?1
         NotLike	        findByFirstnameNotLike                                          	… where x.firstname not like ?1
         StartingWith	    findByFirstnameStartingWith	                                        … where x.firstname like ?1 (parameter bound with appended %)
         EndingWith	        findByFirstnameEndingWith                                       	… where x.firstname like ?1 (parameter bound with prepended %)
         Containing	        findByFirstnameContaining	                                        … where x.firstname like ?1 (parameter bound wrapped in %)
         OrderBy	        findByAgeOrderByLastnameDesc                                    	… where x.age = ?1 order by x.lastname desc
         Not	            findByLastnameNot	                                                … where x.lastname <> ?1
         In	                findByAgeIn(Collection<Age> ages)	                                … where x.age in ?1
         NotIn	            findByAgeNotIn(Collection<Age> age)                             	… where x.age not in ?1
         True	            findByActiveTrue()	                                                … where x.active = true
         False	            findByActiveFalse()	                                                … where x.active = false
         IgnoreCase	        findByFirstnameIgnoreCase                                       	… where UPPER(x.firstame)


 * @Date 下午 4:31 2019/5/29 0029
 * @Param
 * @return
*/
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>,JpaRepository<T,ID>,JpaSpecificationExecutor<T> {


    List<T> findByRequestQuery(RequestQuery query);
    List<T> findByRequestQueryOr(RequestQuery query);
    List<T> findByRequestQueryAndOR(RequestQuery query,HashMap<String,Object> orRelation,Boolean pagesplit);
    List<T> findByRequestQuery(RequestQuery query,Boolean pagesplit);
    List<T> findByRequestQueryOr(RequestQuery query,Boolean pagesplit);
    List<T> findObjectLike(String key,String like);
    List<T> findBySql(String sql);
    List<T> findBySqlArg(String sql, Object...args);
    List<T> findByHQL(String hql);
    List<T> findALLSql();
 /*   Boolean deleteItemByParams(RequestQuery query);
    Boolean deleteItemByParamsOr(RequestQuery query);*/
    @Transactional
    public void updateBySql(String sql, Object...args);
    @Transactional
    public void updateByHql(String hql, Object...args);

}
